package com.nkbh.sevenday.myhttp.http;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by pixel on 2018/4/4.
 * okhttp的网络封装
 */

public class OkHttp implements IHttpClient {


    public static final int GET= 0x3e8;
    public static final int POST= 0x7d0;

    private static OkHttp mInstance;

    private OkHttpClient mOkHttpClient;
    private OkHttp(){
        mOkHttpClient=new OkHttpClient();
    }

    /**
     * 单例模式
     * @return
     */
    public static OkHttp getInstance(){

        if (mInstance==null){
            synchronized (OkHttp.class){
                if (mInstance==null){
                    mInstance=new OkHttp();
                }
            }
        }
        return mInstance;
    }


    @Override
    public void get(String url, final CallBackResponse callBack ) {
        Request request=new Request.Builder().url(url).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBack.faile(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                callBack.success(response,GET);
            }
        });
    }

    @Override
    public void post(String url, Map<String,String> map, final CallBackResponse callBack) {

        FormBody.Builder builder=new FormBody.Builder();

        for (Map.Entry entry:map.entrySet()) {
            String key = (String) entry.getKey();
            String value=entry.getValue().toString();
            builder.add(key,value);
        }
        FormBody formBody = builder.build();
        Request request=new Request.Builder().url(url).post(formBody).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBack.faile(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                callBack.success(response,POST);
            }
        });

    }

    @Override
    public void post(String url, String name, String pwd, final CallBackResponse callBack) {

        FormBody.Builder builder=new FormBody.Builder();
        FormBody formBody = builder
                .add("name",name)
                .add("pwd",pwd)
                .build();
        Request request=new Request.Builder().url(url).post(formBody).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBack.faile(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                callBack.success(response,POST);
            }
        });

    }

    @Override
    public void postUpload(String url, String filePath, String name, String fileType, final CallBackResponse callBack) {
        File file=new File(filePath+ File.separator+name);
        MediaType mediaType=MediaType.parse(fileType);
        RequestBody body=RequestBody.create(mediaType,file);
        Request request=new Request.Builder().url(url).post(body).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBack.faile(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                callBack.success(response,POST);
            }
        });



    }

}
